//
//  LoginEmailViewController.m
//  insyte
//
//  Created by Vu Dang Tuan on 6/19/15.
//  Copyright (c) 2015 Robert. All rights reserved.
//

#import "LoginEmailViewController.h"
#import "UIColor+InsyteColors.h"
#import "Helpers.h"
#import "EmailForgotPasswordViewController.h"
#import "Constants.h"
#import "ServiceManager.h"
#import "RegisterNewAccountWithEmailViewController.h"
#import "ProfileStep1ViewController.h"
#import "ProfilesViewController.h"

@interface LoginEmailViewController ()<UITextFieldDelegate>

@end

@implementation LoginEmailViewController
{
    float _yAs;
    NSMutableArray *_allTextFields;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpNavigationBar];
    [self StyleForView];
    _allTextFields = [[NSMutableArray alloc] init];
    [_allTextFields addObject:self.tfEmail];
    [_allTextFields addObject:self.tfPassword];
    for (UITextField *tf in _allTextFields) {
        tf.delegate = self;
    }
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardWillHideNotification object:nil];
    _yAs = self.view.frame.origin.y;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setUpNavigationBar{
    UIBarButtonItem *btnBack = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:Ic_Back] style:UIBarButtonItemStylePlain target:self action:@selector(OnBackClicked)];
    self.navigationItem.leftBarButtonItem = btnBack;
    self.navigationItem.titleView = nil;
}

- (void) OnBackClicked{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void) StyleForView{
    [Helpers StyleViewWithBorderColor:_viewEmail color:[UIColor textfieldBorderColor] corner:0];
    [Helpers StyleViewWithBorderColor:_viewPassword color:[UIColor textfieldBorderColor] corner:0];
    [Helpers StyleButtonWithBorderColor:_btnLogin color:[UIColor loginButtonColor] corner:0];
    
    self.btnCheckbox.tag = 0;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(dismissKeyboard)];
    
    [self.view addGestureRecognizer:tap];
}

#pragma keyboard

- (void)keyboardDidHide: (NSNotification *) notif{
    [Helpers scrollView:self.view y:_yAs];
}

#pragma Textfield delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if ([UIScreen mainScreen].bounds.size.height < 500) {
        // <= iphone 4s
        if (textField == self.tfEmail) {
            [Helpers scrollView:self.view y:_yAs - 30];
        }
        if (textField == self.tfPassword) {
            [Helpers scrollView:self.view y:_yAs - 40];
        }
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
-(void)dismissKeyboard {
    [self.tfEmail resignFirstResponder];
    [self.tfPassword resignFirstResponder];
}

- (IBAction)OnLoginClicked:(id)sender {
    if (![Helpers isAllTextFieldNotEmpty:_allTextFields]) {
        return;
    }
    if (![Helpers validateEmail:_tfEmail.text]) {
        return;
    }
    [ServiceManager loginWithEmail:_tfEmail.text password:_tfPassword.text cb:^(BOOL rs) {
        if (rs) {
            NSLog(@"here");
            ProfilesViewController *profile = [[ProfilesViewController alloc] init];
            UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:profile];
            [[UIApplication sharedApplication].keyWindow setRootViewController: navController];
        }
    }];
}

- (IBAction)OnForgotPasswordClicked:(id)sender {
    [self.navigationController pushViewController:[[EmailForgotPasswordViewController alloc] init] animated:YES];
}

- (IBAction)OnCheckBoxClicked:(id)sender {
    if (++self.btnCheckbox.tag %2 == 0) {
        [self.btnCheckbox setImage:[UIImage imageNamed:Ic_Check] forState:UIControlStateNormal];
    }else{
        [self.btnCheckbox setImage:[UIImage imageNamed:Ic_Un_Check] forState:UIControlStateNormal];
    }
}
- (IBAction)OnRegisterNewAccount:(id)sender {
    [self.navigationController pushViewController:[[RegisterNewAccountWithEmailViewController alloc] init] animated:YES];
}
@end
